package com.ldk.springboot2021.controller;

import com.ldk.springboot2021.common.JSONResult;
import com.ldk.springboot2021.pojo.Address;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class JsonController {
    @RequestMapping("/toJson1")
    public String toJson1(){
        return "hello";
    }
    @RequestMapping("/toJson2")
    public Address toJson2(){
        Address address=new Address().setAddressId(10).setTel("456").setProvince("广东");
        return address;
    }
    @GetMapping("/toJson3")
    public List<Address> toJson3(){
        List<Address> list=new ArrayList<>();
        Address address1=new Address().setAddressId(10).setTel("456").setProvince("广东");
        Address address2=new Address().setAddressId(10).setTel("456").setProvince("武汉");
        Address address3=new Address().setAddressId(10).setTel("456").setProvince("西安");
        list.add(address1);
        list.add(address2);
        list.add(address3);
        return list;
    }
    //将map转换成json
    @RequestMapping("/toJson4")
    public Map<String,Object> toJson4(){
        Map<String,Object> map=new HashMap<>();
        map.put("state",2000);
        map.put("msg","提交成功");
        Address address=new Address().setAddressId(10).setTel("456").setProvince("广东");
        map.put("data",address);
        return map;
    }

    //自定义数据类型
    @RequestMapping("/toJson5")
    public JSONResult<Address> toJson5(){
//        JSONResult<Address> jsonResult=new JSONResult<>();
        Address address3=new Address().setAddressId(10).setTel("456").setProvince("西安");
        return new JSONResult<>(200,"返回数据成功",address3);
    }
    //自定义数据类型
    @RequestMapping("/toJson6")
    public JSONResult<Void> toJson6(){
//        JSONResult<Address> jsonResult=new JSONResult<>();
        Address address3=new Address().setAddressId(10).setTel("456").setProvince("西安");
        return new JSONResult<>(200,"返回数据成功");
    }
}
